﻿using System.Security.Cryptography;
using System.Text;

namespace Common
{
    /// <summary>
    /// MD5加密
    /// </summary>
    public class Md5
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">加密字符</param>
        /// <param name="code">加密位数16/32</param>
        /// <returns></returns>
        public static string md5(string str, int bit)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(str));
                StringBuilder tmp = new StringBuilder();
                foreach (byte i in data)
                {
                    tmp.Append(i.ToString("x2"));
                }
                if (bit == 16)
                    return tmp.ToString().Substring(8, 16);
                else
                if (bit == 32) return tmp.ToString();//默认情况
                else return string.Empty;
            }      
        }
    }
}
